package ext.tianma.bom.service.sourcingInterface;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.List;


/**
 * @description: Sourcing 调用PLM接口，将品牌相关数据传给PLM
 * @author: MaggieHao
 * @createDate: 2019/8/12 10:23
 * @updateRemark: 修改内容
 * @version: 1.0
 */
@Controller
@RequestMapping({"/sourcingForPLM"})
public class SourcingForPLMController {

    @RequestMapping(value = "/saveBrand", method = {RequestMethod.POST, RequestMethod.GET})
    public void receivedData(@RequestBody String data, HttpServletResponse response) {

        boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
        response.setContentType("text/html;charset=UTF-8");// 返回文本值

        System.out.println("Sourcing传递过来的数据=======" + data);
        try {
            PrintWriter out = response.getWriter();
            String result = "";
            // 逻辑处理
            //将Json转换成对象
            List<SourcingForPLMBean> sourcingForPLMBeanList = JSON.parseObject(data, new TypeReference<List<SourcingForPLMBean>>() {
            });
            //再将转换成的对象保存到数据库中
            SourcingForPLMReturn sourcingForPLMReturn = SourcingForPLMService.saveBrand(sourcingForPLMBeanList);
            //将处理完成的结果对象转换成Json 返回
            result = JSON.toJSONString(sourcingForPLMReturn);
            System.out.println("返回结果result:" + result);
            out.print(result);
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
        }
    }

    /**
     * 查询所有的品牌名称状态 传到页面上
     *
     * @param data
     * @param response
     */
    @RequestMapping(value = "/queryBrandName", method = {RequestMethod.GET})
    public void queryBrandName(@RequestBody String data, HttpServletResponse response, HttpServletRequest request) {

        boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
        response.setContentType("text/html;charset=UTF-8");// 返回文本值

        System.out.println("Sourcing传递过来的数据=======" + data);
        try {
            // 逻辑处理
            PrintWriter out = response.getWriter();
            //再将转换成的对象保存到数据库中
            List<String> brandNames = SourcingForPLMService.queryAllBrandNameByState("合格");
            //将处理完成的结果对象转换成Json 返回

            request.setAttribute("brandNames", brandNames);

            System.out.println("所有的品牌名称:" + brandNames);

            out.print(brandNames);

        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
        }
    }


}
